package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.db.UserInfo;
import com.itheima.mapper.UserInfoMapper;
import com.itheima.service.db.UserInfoService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    //保存用户
    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    //更新用户信息
    @Override
    public void update(UserInfo userInfo) {
        //默认情况下,这是动态sql更新的
        userInfoMapper.updateById(userInfo);
    }

   //根据id查询用户信息
    @Override
    public UserInfo findById(Long userID) {
        return userInfoMapper.selectById(userID);
    }


    //mybatis-plus的分页查询
    @Override
    public PageBeanVo findUserList(Integer pageNum, Integer pageSize) {
        //创建page对象
        IPage<UserInfo> page = new Page<UserInfo>(pageNum, pageSize);//(pageNum-1)*pageSize,pageSize

        //2. 调用mapper,需要将page对象传进去
       QueryWrapper<UserInfo> wrapper = new QueryWrapper<UserInfo>();
       page=userInfoMapper.selectPage(page,wrapper);

        //3. 获取结果
        return new PageBeanVo(pageNum, pageSize, page.getTotal(), page.getRecords());
    }

}
